package com.qianf.qfoffice.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qianf.qfoffice.entity.Grade;
import com.qianf.qfoffice.entity.ResponseData;
import com.qianf.qfoffice.service.GradeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/grade")
public class GradeController {

    @InitBinder
    public void initBinder(WebDataBinder binder) {
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        dateFormat.setLenient(true);
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
    }


    @Autowired
    private GradeService gradeService;

    /**
     * 查询 所有 班级
     * @return
     */
    @RequestMapping("/selectGradeAll.do")
    @ResponseBody
    public ResponseData<Object> selectGradeAll(int page, int limit){
        // 开启分页
        PageHelper.startPage(page, limit);

        List<Grade> grade = gradeService.findAllGrade();

        PageInfo pageInfo = new PageInfo(grade);

        ResponseData<Object> responseData = new ResponseData<>();
        responseData.setCode(0);
        responseData.setMsg("success");
        responseData.setCount((int) pageInfo.getTotal());
        responseData.setData(grade);

        return responseData;

    }

    /**
     * 根据id 删除班级
     * @param id
     * @return
     */
    @RequestMapping("/deleteGradeById")
    @ResponseBody
    public ResponseData deleteGradeById(int id){
        ResponseData<Object> responseData = new ResponseData<>();
        gradeService.deleteGradeById(id);

        responseData.setCode(1);
        responseData.setMsg("删除成功");
        return responseData;

    }

    /**
     * 修改班级
     * @param grade
     * @return
     */
    @RequestMapping("/updateGrade")
    @ResponseBody
    public ResponseData<Object> updateGrade(Grade grade){

        ResponseData<Object> responseData = new ResponseData<>();
        int i = gradeService.updateGrade(grade);
        if (i > 0){
            responseData.setCode(1);
            responseData.setMsg("修改成功");
            return responseData;
        }else {
            responseData.setCode(0);
            responseData.setMsg("修改失败");
            return responseData;
        }

    }

    /**
     * 添加班级
     * @param grade
     * @return
     */
    @RequestMapping("/insertGrade.do")
    @ResponseBody
    public ResponseData insertGrade(Grade grade){

        ResponseData<Object> responseData = new ResponseData<>();

        int i = gradeService.addGrade(grade);
        if (i > 0){
            responseData.setCode(1);
            responseData.setMsg("添加成功");
            return responseData;
        }else {
            responseData.setCode(0);
            responseData.setMsg("添加失败");
            return responseData;
        }

    }
}
